#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;

int n, rt;
struct Node {
  int f, sz, sum;
  double avg;
} a[N];
int find() {
  double avg = 0;
  int res = -1;
  rep(i, 1, n) {
    if (i != rt && avg < a[i].avg) avg = a[i].avg, res = i;
  }
  return res;
}
int main() {
  cin >> n >> rt;
  int res = 0;
  rep(i, 1, n) {
    cin >> a[i].sum;
    a[i].sz = 1;
    a[i].avg = a[i].sum;
    res += a[i].sum;
  }
  rep(i, 2, n) {
    int u, v;
    cin >> u >> v;
    a[v].f = u;
  }

  rep(i, 2, n) {
    int v = find();
    int f = a[v].f;
    res += a[v].sum * a[f].sz;
    a[v].avg = -1;
    rep(j, 1, n) {
      if (a[j].f == v) a[j].f = f;
    }
    a[f].sum += a[v].sum;
    a[f].sz += a[v].sz;
    a[f].avg = 1.0 * a[f].sum / a[f].sz;
  }
  cout << res;
  return 0;
}